<?php

namespace app\common;

use app\model\activity\free\StoreFree;
use app\model\product\sku\StoreProductAttrValue;

class FreeTool
{

    /**
     * @return FreeTool
     * 静态调用入口
     */
    public static function init() : FreeTool
    {
        return new static();
    }

    /**
     *
     * 获取一个系统默认的满赠
     *
     * @param $unique
     * @param $has_seckill
     * @param $has_coupon
     * @return array
     */
    public function getAvailableFree($unique, $has_seckill = false, $has_coupon = false)
    {

        $query = StoreFree::with(['productRelations.attrInfo', 'rules'])->when($has_seckill && !$has_coupon, function ($query) {
            $query->whereIn('add_marketing_type', [1, 3]);
        })->when($has_coupon && !$has_seckill, function ($query) {
            $query->whereIn('add_marketing_type', [2, 3]);
        })->when($has_seckill && $has_coupon, function ($query) {
            $query->whereIn('add_marketing_type', [3]);
        })->when(!$has_coupon && !$has_seckill, function ($query) {
            $query->whereIn('add_marketing_type', [0]);
        })->where('start_time', '<=', time())->where('end_time', '>', time())
            ->where('is_del', 0)->where('status', 1);

        $part_query = clone $query;
        // 1. 找出能适用所有商品的满赠
        $type_all = $query->where('is_use_all_product', 1)->select();
        // 2. 找出适应部分优惠券的满赠
        $type_part = $part_query->hasWhere('productRelations', function ($query) use ($unique) {
            $query->where('product_attr_value_unique', $unique);
        })->where('is_use_all_product', 0)->select();

        // 3. 合并满赠
        return $type_all->merge($type_part);



    }

    /**
     *
     * 获取默认的满赠
     *
     * @param $unique
     * @param $price
     * @param $nums
     * @param $has_seckill
     * @param $has_coupon
     * @return array
     *
     */
    public function pickDefaultAvailableFree($unique, $price = 0, $nums = 0, $has_seckill = false, $has_coupon = false)
    {
        $storeFrees = $this->getAvailableFree($unique, $has_seckill, $has_coupon);
        $free_info = [];
        foreach ($storeFrees as $item) {
            // 4.1 需要从最大角度去判断
            $max_price_free = $item->rules()->where('type', 1)->where('min_price', '<=', $price)->order('min_price desc')->find();
            $max_count_free = $item->rules()->where('type', 2)->where('min_count', '<=', $nums)->order('min_count desc')->find();

            $pick_free = ($max_price_free) ?: $max_count_free;

            if ($pick_free) {
                $free_rule_products = $pick_free->products()->where('activity_count', '>', 0)->limit($pick_free->nums)->field(['free_rule_id', 'product_attr_value_unique', 'give_count', 'activity_count'])->select();

                $products = [];
                foreach ($free_rule_products as $free_rule_product) {
                    $productAttrValue = StoreProductAttrValue::with(['product'])->where('unique', $free_rule_product->product_attr_value_unique)->find();

                    $products[] = [
                        'free_rule_id'  =>  $pick_free->id,
                        "store_name"=> ($productAttrValue->product) ? $productAttrValue->product->store_name : '',
                        "unique"=>  $value['product_attr_value_unique'] ?? '',
                        "suk"=> $productAttrValue->suk ?? '',
                        "image"=> $productAttrValue->image ?? '',
                        "price"=> $productAttrValue->price ?? '',
                        "show_price"=> "0.00",
                        "give_count"=> $free_rule_product->give_count
                    ];
                }

                $free_info[] = [
                    'id'    =>  $pick_free->id,
                    'nums'  =>  $pick_free->nums,
                    'list'  =>  $products
                ];
            }

        }

        return $free_info;
    }
}